#ifndef LOGGER_H
#define LOGGER_H

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <iomanip>
#include <mutex>
#include <thread>
#include <queue>
#include <condition_variable>
#include <atomic>
#include <sstream>
#include <filesystem>

class Logger {
public:
    enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        ERROR
    };
    // 获取单例实例
    static Logger& getInstance(const std::string& baseFilename = "", LogLevel level = INFO);

    Logger(const Logger&) = delete;
    Logger& operator=(const Logger&) = delete;

    void debug(const std::string& message);
    void info(const std::string& message);
    void warn(const std::string& message);
    void error(const std::string& message);

    void setLogLevel(LogLevel level);

private:
    Logger(const std::string& baseFilename, LogLevel level);
    ~Logger();
    
    // 获取当前时间
    std::string getCurrentTime();
    std::string getCurrentDate();
    
    // 日志级别转换为字符串
    std::string levelToString(LogLevel level);

    void openLogFile();
    void checkLogFile();
    void log(LogLevel level, const std::string& message);
    void processLog();

    bool checkDir();

    std::ofstream logFile;
    std::mutex logMutex;
    std::mutex queueMutex;
    std::condition_variable cv;
    std::queue<std::string> logQueue;
    std::thread logThread;
    LogLevel logLevel;
    std::string baseFilename;
    std::string currentDate;
    std::atomic<bool> running;
    std::string strBaseDir;
};

#endif
